const Koa = require("koa");
const Router = require("koa-router");
const Static = require("koa-static");
const bodyParser = require("koa-bodyparser");
const config = require("./routers/config/default");
const cors = require('koa-cors')
// 赵孟凡---写的接口
const routes = require("./routers/api");



const { test } = require("./routers/middleware/test");

// 实例化koa
const app = new Koa();
// 实例化koa-Router
const router = new Router();
app.use(test);
app.use(bodyParser());
app.use(cors())

app.use(Static(__dirname + "/public"));
app.use(async (ctx, next) => {
  ctx.set("Access-Control-Allow-Origin", "*");
  ctx.set("Access-Control-Allow-Headers", "Content-Type");
  ctx.set("Access-Control-Allow-Methods", "POST,GET, OPTIONS, PUT");
  await next();
});
//

// 配置路由地址localhost:4000/indexRouter/api/koaRouter
// 赵孟凡---配置的路由地址
router.use("/routes/api/index", routes);
// 配置路由
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(config.port, () => {
  console.log("server started on 3307");
});
// generator
